package com.sc.everyonerentboot.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sc.everyonerentboot.mapper.*;
import com.sc.everyonerentboot.pojo.*;
import com.sc.everyonerentboot.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service("ps")
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductsMapper mapper;
    @Autowired
    ProductImagesMapper imgmapper;
    @Autowired
    ComputerParametersMapper commapper;
    @Autowired
    PhoneTabletParametersMapper phomapper;
    @Autowired
    HeadsetParametersMapper heamapper;
    @Transactional
    public int add(Products p) {
        if (p.getProductImages()!=null){
            for(ProductImages img:p.getProductImages()){
                imgmapper.insertSelective(img);
            }
        }
        String parameterId=UUID.randomUUID().toString();
        p.setParameterInfoId(parameterId);
        if ("computer".equals(p.getProductType())){
            ComputerParameters parameters=(ComputerParameters)p.getParameter();
            parameters.setId(parameterId);
            commapper.insertSelective((ComputerParameters) p.getParameter());
        }else if("phone".equals(p.getProductType())){
            PhoneTabletParameters parameters=(PhoneTabletParameters)p.getParameter();
            parameters.setId(parameterId);
            phomapper.insertSelective((PhoneTabletParameters) p.getParameter());
        }else if("headset".equals(p.getProductType())){
            HeadsetParameters parameters=(HeadsetParameters)p.getParameter();
            parameters.setId(parameterId);
            heamapper.insertSelective((HeadsetParameters) p.getParameter());
        }
        return mapper.insertSelective(p);
    }

    @Transactional
    public int del(Integer id) {
        Products product=mapper.selectByPrimaryKey(id);
        String parameterInfoId
                =product.getParameterInfoId();
        int i= mapper.deleteByPrimaryKey(id);
        if (product.getProductImages()!=null){
            for(ProductImages img:product.getProductImages()){
                imgmapper.delByProductId(img.getId());
            }
        }

        commapper.deleteByPrimaryKey(parameterInfoId);
        phomapper.deleteByPrimaryKey(parameterInfoId);
        heamapper.deleteByPrimaryKey(parameterInfoId);
        return i;
    }

    @Transactional
    public int delAll(Integer[] ids) {
        int sum=0;
        for (Integer id :ids){
            Products products
                    =mapper.selectByPrimaryKey(id);
            String parameterInfoId=products.getParameterInfoId();
            sum+=mapper.deleteByPrimaryKey(id);

            if (products.getProductImages()!=null){
                for(ProductImages img:products.getProductImages()){
                    imgmapper.delByProductId(img.getId());
                }
            }
            commapper.deleteByPrimaryKey(parameterInfoId);
            phomapper.deleteByPrimaryKey(parameterInfoId);
            heamapper.deleteByPrimaryKey(parameterInfoId);
        }
        return sum;
    }

    @Override
    public int update(Products p) {
        if ("computer".equals(p.getProductType())){
            String id=p.getParameterInfoId();
            ComputerParameters parameters=(ComputerParameters) p.getParameter();
            parameters.setId(id);
            System.out.println(id);
            commapper.updateByPrimaryKeySelective(parameters);
        }else if("phone".equals(p.getProductType())){
            phomapper.updateByPrimaryKeySelective((PhoneTabletParameters) p.getParameter());
        }else if("headset".equals(p.getProductType())){
            heamapper.updateByPrimaryKeySelective((HeadsetParameters) p.getParameter());
        }
        return mapper.updateByPrimaryKeySelective(p);
    }

    @Override
    public PageInfo<Products> showAll(Integer pageNum, Integer pageSize, Products p) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(mapper.selectAll(p));
    }
}
